package co.crossoft.daoImpl;

import co.crossoft.base.common.JsonResult;
import co.crossoft.domain.MiniShowBean;
import co.crossoft.domain.ResourceType;
import co.crossoft.domain.ResourceTypeResource;
import co.crossoft.mapper.ResourceTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Snow on 8/24/2017.
 */
@Repository("typeResourceDaoImpl")
public class TypeResourceDaoImpl {

    @Autowired
    ResourceTypeMapper resourceTypeMapper;

    public JsonResult addOrUpdateResource(ResourceType resourceType, ArrayList<String> resourceHandleList) {
        JsonResult jsonResult = new JsonResult("S","success");
        String resourceTypeHandle = resourceType.getHandle();
        List<ResourceType> typeList = resourceTypeMapper.selectByHandle(resourceType.getHandle());
        if (typeList.size() > 0) {
            resourceTypeMapper.updateResourceType(resourceType);
        } else {
            resourceTypeMapper.addResourceType(resourceType);
        }
        resourceTypeMapper.deleteAllResTypeAss(resourceType.getHandle());
        if (resourceHandleList.size() > 0) {
            List<ResourceTypeResource> addList = new ArrayList<>();
            for (String resourceHandle : resourceHandleList) {
                ResourceTypeResource resourceTypeResource = new ResourceTypeResource();
                resourceTypeResource.setHandle(resourceTypeHandle + ":" + resourceHandle);
                resourceTypeResource.setResourceFk(resourceHandle);
                resourceTypeResource.setResourceTypeFk(resourceTypeHandle);
                addList.add(resourceTypeResource);
            }
            resourceTypeMapper.addResTypeAss(addList);
        }
        return jsonResult;
    }

    public JsonResult deleteResourceType(String resourceTypeHandle) {
        JsonResult jsonResult = new JsonResult("S","success");
        if (resourceTypeHandle.equals("")) {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("Resource type handle can not be handle, please execute search before delete");
        } else {
            resourceTypeMapper.deleteByHandle(resourceTypeHandle);
            resourceTypeMapper.deleteAllResTypeAss(resourceTypeHandle);
        }
        return jsonResult;
    }

    public JsonResult searchDataByResourceType(String resourceTypeName) {
        JsonResult jsonResult = new JsonResult("S","success");
        Map parMap = new HashMap();
        parMap.put("resourceTypeName", resourceTypeName);
        List<ResourceType> list = resourceTypeMapper.selectByResType(parMap);
        jsonResult.setResult(list);
        return jsonResult;
    }

    public JsonResult searchOneResourceTypeByHandle(String resourceTypHandle) {
        JsonResult jsonResult = new JsonResult("S","success");
        List<ResourceType> resTypeList = resourceTypeMapper.selectByHandle(resourceTypHandle);
        if (resTypeList.size() > 0) {
            List<MiniShowBean> AssResList = resourceTypeMapper.assResourceList(resourceTypHandle);
            String plant = (resourceTypHandle.split(":")[1]).split(",")[0];
            List<MiniShowBean> UnAssResList = resourceTypeMapper.unAssResourceList(plant);
            Map retMap = new HashMap<String, List>();
            retMap.put("ResTypeList", resTypeList);
            retMap.put("AssResList", AssResList);
            retMap.put("UnAssResList", UnAssResList);
            jsonResult.setResult(retMap);
        } else {
            jsonResult.setResultCode("F");
            jsonResult.setResultMsg("no data can be founded");
        }
        return jsonResult;
    }
}
